<script setup>
import Toast from '@vant/weapp/dist/toast/toast';
import tipPopup from './child/tipPopup.vue';
import { getAiImgRecordInfo } from "@/api/painting";

import { ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';



// 页面携带参数
const queryItem = ref({})

const isVideoShow = ref(true)
const tipPopupShow = ref(true)



function downloadFn() {
    saveVideo()
    tipPopupShow.value = false
}

// 视频播放错误处理
function videoErrorFn() {
    isVideoShow.value = false
}



function prevViewImgFn() {
    uni.previewImage({
        urls: [queryItem.value.orgImgPath],
        current: queryItem.value.orgImgPath
    })
}


function accredit() {
    return new Promise(async (resolve, reject) => {
        // 获取已经获得的权限列表
        const res = await uni.getSetting();
        // 已获得存储相册授权
        if (res.authSetting["scope.writePhotosAlbum"]) {
            return resolve(true);
        } else if (res.authSetting["scope.writePhotosAlbum"] === undefined) {
            // 未获得存储相册授权
            uni.authorize({
                scope: "scope.writePhotosAlbum",
                success(res) {
                    resolve(res);
                },
            });
        } else {
            uni.openSetting();
            reject(false);
        }

    })
};


// 保存图片
async function saveVideo() {
    Toast.loading({
        message: '加载中',
        duration: 0,
        forbidClick: true,
    });
    try {
        await accredit()
        let filePath = Array.isArray(queryItem.value.imgPath) ? queryItem.value.imgPath[0] : queryItem.value.imgPath;
        uni.downloadFile({
            url: filePath,
            success: (res) => {
                uni.saveVideoToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: () => {
                        Toast.success('保存成功');
                    },
                    fail: (err) => {
                        Toast.fail('保存失败');
                    }
                });
            },
            fail: (err) => {
                Toast.fail('下载失败');
            }
        })
    } catch (error) {

    }
}


// 获取详情信息
async function getAiImgRecordInfoFn() {
    const res = await getAiImgRecordInfo(queryItem.value.id)
    queryItem.value = Object.assign(queryItem.value, res.data)
}



onLoad((e) => {
    if (e.item) {
        queryItem.value = JSON.parse(e.item);
        getAiImgRecordInfoFn()
    }
})
</script>

<template>
    <div class='bgRedrawingDetail'>
        <NavBar>{{ queryItem?.title }}</NavBar>
        <div class="container">
            <div class="video_box">
                <video :src="queryItem?.imgPath" @error="videoErrorFn" play-btn-position="center" class="video"
                    v-if="isVideoShow"></video>
                <img :src="queryItem?.imgTempPath" alt="" class="video_poster" mode="aspectFill" @click="prevViewImgFn"
                    v-else>
            </div>
            <div class="tips_box">
                <i class="iconfont ai-icon-jinggao"></i>
                视频仅在云端存储24小时，请及时下载到本地哦
            </div>
            <div class="info_box">
                <div class="label">描述词：</div>
                <div class="text">{{ queryItem?.promptContent }}</div>

                <div class="label">视频信息</div>
                <div class="text">
                    <span>详情信息</span>
                    <span class="num">{{ queryItem?.style }}</span>
                </div>
                <div class="text">
                    <span>生成时间</span>
                    <span class="num">{{ queryItem?.createTime }}</span>
                </div>

            </div>
        </div>
        <div class="footer">
            <div class="footer_placeholder"></div>
            <div class="footer_content">
                <div class="right" @click="saveVideo">保存视频</div>
            </div>
        </div>
    </div>
    <van-toast id="van-toast" />
    <tipPopup v-model:show="tipPopupShow" @download="downloadFn"></tipPopup>
</template>

<style lang='scss' scoped>
.bgRedrawingDetail {
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .container {
        flex: 1;
        padding: 23rpx 30rpx 0;

        .video_box {
            height: 345px;

            .video {
                width: 100%;
                height: 100%;
                border-radius: 20rpx;
                background: var(--a-main-box-bg-color);
            }

            .video_poster {
                width: 100%;
                height: 100%;
                border-radius: 20rpx;
            }
        }

        .tips_box {
            display: flex;
            align-items: center;
            gap: 10rpx;
            color: var(--a-one-text-color);
            margin-top: 20rpx;
            font-size: 26rpx;

            .iconfont {
                color: var(--a-main-color);
                font-size: 24rpx;
            }
        }

        .info_box {
            margin-top: 32rpx;
            color: var(--a-one-text-color);

            .label {
                font-size: 24rpx;
                margin-top: 32rpx;

                &.tip {
                    font-size: 32rpx;
                }

                &::before {
                    content: "";
                    display: inline-block;
                    width: 12rpx;
                    height: 18rpx;
                    background: var(--a-main-color);
                    margin-right: 10rpx;
                    border-radius: 6rpx;
                }
            }

            .text {
                font-size: 28rpx;
                color: var(--a-one-text-color);
                display: flex;
                align-items: center;
                justify-content: space-between;
                margin-top: 16rpx;

                .num {
                    font-size: 24rpx;
                    color: #FFFFFF;
                }
            }
        }
    }

    .footer {
        .footer_placeholder {
            height: 176rpx;
        }

        .footer_content {
            box-sizing: border-box;
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 176rpx;
            background: var(--a-main-tabNav-bg-color);
            padding: 28rpx 30rpx 0;
            display: flex;
            align-items: flex-start;
            justify-content: center;
            gap: 24rpx;


            .right {
                flex-shrink: 0;
                width: 375rpx;
                height: 84rpx;
                border-radius: 42rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                color: var(--a-one-text-color);
                background-color: var(--a-main-color);
            }

            .left {
                width: 100%;
            }
        }
    }
}
</style>